//index.js
//获取应用实例
const app = getApp()

var util = require('../../utils/util.js')
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
Page({
  data: {
    standardList: [{
        "code": 40,
        "text1": 11.54,
        "text2": 13.04,
        "text3": 12.00,
        "text4": 9.60,
        "text5": 2.86,
        "text6": 2.40
      },
      {
        "code": 39.5,
        "text1": 11.59,
        "text2": 13.14,
        "text3": 11.89,
        "text4": 9.50,
        "text5": 2.84,
        "text6": 2.38
      },
      {
        "code": 39,
        "text1": 11.64,
        "text2": 13.24,
        "text3": 11.78,
        "text4": 9.40,
        "text5": 2.83,
        "text6": 2.37
      },
      {
        "code": 38.5,
        "text1": 11.69,
        "text2": 13.34,
        "text3": 11.67,
        "text4": 9.30,
        "text5": 2.81,
        "text6": 2.36
      },
      {
        "code": 38,
        "text1": 11.74,
        "text2": 13.44,
        "text3": 11.56,
        "text4": 9.20,
        "text5": 2.80,
        "text6": 2.35
      },
      {
        "code": 37.5,
        "text1": 11.79,
        "text2": 13.54,
        "text3": 11.45,
        "text4": 9.10,
        "text5": 2.79,
        "text6": 2.33
      },
      {
        "code": 37,
        "text1": 11.84,
        "text2": 13.64,
        "text3": 11.34,
        "text4": 9.00,
        "text5": 2.78,
        "text6": 2.32
      },
      {
        "code": 36.5,
        "text1": 11.89,
        "text2": 13.74,
        "text3": 11.23,
        "text4": 8.90,
        "text5": 2.77,
        "text6": 2.31
      },
      {
        "code": 36,
        "text1": 11.94,
        "text2": 13.84,
        "text3": 11.12,
        "text4": 8.80,
        "text5": 2.75,
        "text6": 2.30
      },
      {
        "code": 35.5,
        "text1": 11.99,
        "text2": 13.94,
        "text3": 11.02,
        "text4": 8.71,
        "text5": 2.74,
        "text6": 2.29
      },
      {
        "code": 35,
        "text1": 12.04,
        "text2": 14.04,
        "text3": 10.92,
        "text4": 8.62,
        "text5": 2.73,
        "text6": 2.27
      },
      {
        "code": 34.5,
        "text1": 12.09,
        "text2": 14.14,
        "text3": 10.82,
        "text4": 8.53,
        "text5": 2.71,
        "text6": 2.26
      },
      {
        "code": 34,
        "text1": 12.14,
        "text2": 14.24,
        "text3": 10.72,
        "text4": 8.44,
        "text5": 2.70,
        "text6": 2.25
      },
      {
        "code": 33.5,
        "text1": 12.19,
        "text2": 14.34,
        "text3": 10.62,
        "text4": 8.35,
        "text5": 2.69,
        "text6": 2.24
      },
      {
        "code": 33,
        "text1": 12.23,
        "text2": 14.44,
        "text3": 10.52,
        "text4": 8.26,
        "text5": 2.68,
        "text6": 2.22
      },
      {
        "code": 32.5,
        "text1": 12.28,
        "text2": 14.54,
        "text3": 10.42,
        "text4": 8.17,
        "text5": 2.66,
        "text6": 2.21
      },
      {
        "code": 32,
        "text1": 12.32,
        "text2": 14.64,
        "text3": 10.32,
        "text4": 8.08,
        "text5": 2.65,
        "text6": 2.20
      },
      {
        "code": 31.5,
        "text1": 12.37,
        "text2": 14.74,
        "text3": 10.22,
        "text4": 7.99,
        "text5": 2.64,
        "text6": 2.19
      },
      {
        "code": 31,
        "text1": 12.41,
        "text2": 14.84,
        "text3": 10.12,
        "text4": 7.90,
        "text5": 2.62,
        "text6": 2.17
      },
      {
        "code": 30.5,
        "text1": 12.46,
        "text2": 14.94,
        "text3": 10.02,
        "text4": 7.81,
        "text5": 2.61,
        "text6": 2.16
      },
      {
        "code": 30,
        "text1": 12.50,
        "text2": 15.04,
        "text3": 9.92,
        "text4": 7.72,
        "text5": 2.60,
        "text6": 2.15
      },
      {
        "code": 29.5,
        "text1": 12.55,
        "text2": 15.14,
        "text3": 9.82,
        "text4": 7.63,
        "text5": 2.59,
        "text6": 2.14
      },
      {
        "code": 29,
        "text1": 12.59,
        "text2": 15.24,
        "text3": 9.72,
        "text4": 7.54,
        "text5": 2.57,
        "text6": 2.12
      },
      {
        "code": 28.5,
        "text1": 12.64,
        "text2": 15.34,
        "text3": 9.61,
        "text4": 7.45,
        "text5": 2.56,
        "text6": 2.11
      },
      {
        "code": 28,
        "text1": 12.68,
        "text2": 15.44,
        "text3": 9.50,
        "text4": 7.36,
        "text5": 2.55,
        "text6": 2.10
      },
      {
        "code": 27.5,
        "text1": 12.73,
        "text2": 15.54,
        "text3": 9.40,
        "text4": 7.27,
        "text5": 2.54,
        "text6": 2.09
      },
      {
        "code": 27,
        "text1": 12.77,
        "text2": 15.64,
        "text3": 9.30,
        "text4": 7.18,
        "text5": 2.52,
        "text6": 2.07
      },
      {
        "code": 26.5,
        "text1": 12.82,
        "text2": 15.74,
        "text3": 9.20,
        "text4": 7.09,
        "text5": 2.51,
        "text6": 2.06
      },
      {
        "code": 26,
        "text1": 12.86,
        "text2": 15.84,
        "text3": 9.10,
        "text4": 7.00,
        "text5": 2.50,
        "text6": 2.05
      },
      {
        "code": 25.5,
        "text1": 12.91,
        "text2": 15.94,
        "text3": 9.00,
        "text4": 6.90,
        "text5": 2.48,
        "text6": 2.03
      },
      {
        "code": 25,
        "text1": 12.95,
        "text2": 16.04,
        "text3": 8.90,
        "text4": 6.80,
        "text5": 2.47,
        "text6": 2.02
      },
      {
        "code": 24.5,
        "text1": 13.00,
        "text2": 16.14,
        "text3": 8.80,
        "text4": 6.70,
        "text5": 2.46,
        "text6": 2.01
      },
      {
        "code": 24,
        "text1": 13.04,
        "text2": 16.24,
        "text3": 8.70,
        "text4": 6.60,
        "text5": 2.45,
        "text6": 2.00
      },
      {
        "code": 23.5,
        "text1": 13.09,
        "text2": 16.34,
        "text3": 8.59,
        "text4": 6.51,
        "text5": 2.43,
        "text6": 1.98
      },
      {
        "code": 23,
        "text1": 13.14,
        "text2": 16.44,
        "text3": 8.48,
        "text4": 6.42,
        "text5": 2.42,
        "text6": 1.97
      },
      {
        "code": 22.5,
        "text1": 13.19,
        "text2": 16.54,
        "text3": 8.37,
        "text4": 6.33,
        "text5": 2.41,
        "text6": 1.96
      },
      {
        "code": 22,
        "text1": 13.24,
        "text2": 16.64,
        "text3": 8.26,
        "text4": 6.24,
        "text5": 2.39,
        "text6": 1.94
      },
      {
        "code": 21.5,
        "text1": 13.29,
        "text2": 16.74,
        "text3": 8.15,
        "text4": 6.15,
        "text5": 2.38,
        "text6": 1.93
      },
      {
        "code": 21,
        "text1": 13.34,
        "text2": 16.84,
        "text3": 8.05,
        "text4": 6.06,
        "text5": 2.37,
        "text6": 1.92
      },
      {
        "code": 20.5,
        "text1": 13.39,
        "text2": 16.94,
        "text3": 7.95,
        "text4": 5.97,
        "text5": 2.35,
        "text6": 1.90
      },
      {
        "code": 20,
        "text1": 13.44,
        "text2": 17.04,
        "text3": 7.85,
        "text4": 5.88,
        "text5": 2.34,
        "text6": 1.89
      },
      {
        "code": 19.5,
        "text1": 13.49,
        "text2": 17.14,
        "text3": 7.74,
        "text4": 5.79,
        "text5": 2.33,
        "text6": 1.88
      },
      {
        "code": 19,
        "text1": 13.54,
        "text2": 17.24,
        "text3": 7.63,
        "text4": 5.70,
        "text5": 2.32,
        "text6": 1.86
      },
      {
        "code": 18.5,
        "text1": 13.59,
        "text2": 17.34,
        "text3": 7.52,
        "text4": 5.61,
        "text5": 2.30,
        "text6": 1.85
      },
      {
        "code": 18,
        "text1": 13.64,
        "text2": 17.44,
        "text3": 7.41,
        "text4": 5.52,
        "text5": 2.29,
        "text6": 1.84
      },
      {
        "code": 17.5,
        "text1": 13.69,
        "text2": 17.54,
        "text3": 7.30,
        "text4": 5.43,
        "text5": 2.28,
        "text6": 1.82
      },
      {
        "code": 17,
        "text1": 13.74,
        "text2": 17.64,
        "text3": 7.20,
        "text4": 5.34,
        "text5": 2.26,
        "text6": 1.81
      },
      {
        "code": 16.5,
        "text1": 13.79,
        "text2": 17.74,
        "text3": 7.10,
        "text4": 5.25,
        "text5": 2.25,
        "text6": 1.80
      },
      {
        "code": 16,
        "text1": 13.84,
        "text2": 17.84,
        "text3": 7.00,
        "text4": 5.16,
        "text5": 2.24,
        "text6": 1.78
      },
      {
        "code": 15.5,
        "text1": 13.88,
        "text2": 17.94,
        "text3": 6.90,
        "text4": 5.07,
        "text5": 2.23,
        "text6": 1.77
      },
      {
        "code": 15,
        "text1": 13.93,
        "text2": 18.04,
        "text3": 6.80,
        "text4": 4.98,
        "text5": 2.21,
        "text6": 1.76
      },
      {
        "code": 14.5,
        "text1": 13.97,
        "text2": 18.14,
        "text3": 6.70,
        "text4": 4.89,
        "text5": 2.20,
        "text6": 1.74
      },
      {
        "code": 14,
        "text1": 14.02,
        "text2": 18.24,
        "text3": 6.60,
        "text4": 4.80,
        "text5": 2.19,
        "text6": 1.73
      },
      {
        "code": 13.5,
        "text1": 14.06,
        "text2": 18.34,
        "text3": 6.50,
        "text4": 4.70,
        "text5": 2.17,
        "text6": 1.72
      },
      {
        "code": 13,
        "text1": 14.11,
        "text2": 18.44,
        "text3": 6.40,
        "text4": 4.60,
        "text5": 2.16,
        "text6": 1.70
      },
      {
        "code": 12.5,
        "text1": 14.15,
        "text2": 18.54,
        "text3": 6.30,
        "text4": 4.50,
        "text5": 2.15,
        "text6": 1.69
      },
      {
        "code": 12,
        "text1": 14.20,
        "text2": 18.64,
        "text3": 6.20,
        "text4": 4.40,
        "text5": 2.14,
        "text6": 1.67
      },
      {
        "code": 11.5,
        "text1": 14.24,
        "text2": 18.74,
        "text3": 6.09,
        "text4": 4.30,
        "text5": 2.12,
        "text6": 1.66
      },
      {
        "code": 11,
        "text1": 14.29,
        "text2": 18.84,
        "text3": 5.98,
        "text4": 4.20,
        "text5": 2.11,
        "text6": 1.65
      },
      {
        "code": 10.5,
        "text1": 14.33,
        "text2": 18.94,
        "text3": 5.87,
        "text4": 4.10,
        "text5": 2.10,
        "text6": 1.63
      },
      {
        "code": 10,
        "text1": 14.38,
        "text2": 19.04,
        "text3": 5.76,
        "text4": 4.00,
        "text5": 2.08,
        "text6": 1.62
      },
      {
        "code": 9.5,
        "text1": 14.42,
        "text2": 19.14,
        "text3": 5.65,
        "text4": 3.90,
        "text5": 2.07,
        "text6": 1.61
      },
      {
        "code": 9,
        "text1": 14.47,
        "text2": 19.24,
        "text3": 5.54,
        "text4": 3.80,
        "text5": 2.06,
        "text6": 1.59
      },
      {
        "code": 8.5,
        "text1": 14.51,
        "text2": 19.34,
        "text3": 5.43,
        "text4": 3.70,
        "text5": 2.05,
        "text6": 1.58
      },
      {
        "code": 8,
        "text1": 14.56,
        "text2": 19.44,
        "text3": 5.32,
        "text4": 3.60,
        "text5": 2.03,
        "text6": 1.56
      },
      {
        "code": 7.5,
        "text1": 14.60,
        "text2": 19.54,
        "text3": 5.21,
        "text4": 3.50,
        "text5": 2.02,
        "text6": 1.55
      },
      {
        "code": 7,
        "text1": 14.65,
        "text2": 19.64,
        "text3": 5.10,
        "text4": 3.40,
        "text5": 2.01,
        "text6": 1.53
      },
      {
        "code": 6.5,
        "text1": 14.69,
        "text2": 19.74,
        "text3": 5.00,
        "text4": 3.30,
        "text5": 1.99,
        "text6": 1.52
      },
      {
        "code": 6,
        "text1": 14.74,
        "text2": 19.84,
        "text3": 4.90,
        "text4": 3.20,
        "text5": 1.98,
        "text6": 1.51
      },
      {
        "code": 5.5,
        "text1": 14.79,
        "text2": 19.94,
        "text3": 4.80,
        "text4": 3.10,
        "text5": 1.97,
        "text6": 1.49
      },
      {
        "code": 5,
        "text1": 14.84,
        "text2": 20.04,
        "text3": 4.70,
        "text4": 3.00,
        "text5": 1.95,
        "text6": 1.48
      },
      {
        "code": 4.5,
        "text1": 14.89,
        "text2": 20.14,
        "text3": 4.60,
        "text4": 2.90,
        "text5": 1.94,
        "text6": 1.46
      },
      {
        "code": 4,
        "text1": 14.94,
        "text2": 20.24,
        "text3": 4.50,
        "text4": 2.80,
        "text5": 1.93,
        "text6": 1.45
      },
      {
        "code": 3.5,
        "text1": 14.99,
        "text2": 20.34,
        "text3": 4.40,
        "text4": 2.70,
        "text5": 1.92,
        "text6": 1.44
      },
      {
        "code": 3,
        "text1": 15.04,
        "text2": 20.44,
        "text3": 4.30,
        "text4": 2.60,
        "text5": 1.90,
        "text6": 1.43
      },
      {
        "code": 2.5,
        "text1": 15.09,
        "text2": 20.54,
        "text3": 4.20,
        "text4": 2.50,
        "text5": 1.89,
        "text6": 1.41
      },
      {
        "code": 2,
        "text1": 15.14,
        "text2": 20.64,
        "text3": 4.10,
        "text4": 2.40,
        "text5": 1.88,
        "text6": 1.39
      },
      {
        "code": 1.5,
        "text1": 15.19,
        "text2": 20.74,
        "text3": 4.00,
        "text4": 2.30,
        "text5": 1.86,
        "text6": 1.38
      },
      {
        "code": 1,
        "text1": 15.24,
        "text2": 20.84,
        "text3": 3.90,
        "text4": 2.20,
        "text5": 1.85,
        "text6": 1.36
      },
    ],
    /*successList: [
      {
        id: util.wxuuid(),
        projectItem: 1,
        success: 10,
        score: 10,
        show: false
      },
      {
        id: util.wxuuid(),
        projectItem: 2,
        success: 12,
        score: 5,
        show: false
      }
    ],*/
    successList: [],
    //触摸坐标初始化
    touchStartX: 0,
    show: false,
    projectItems: [{
        text: '请选择项目',
        value: 0
      },
      {
        text: '100米男成绩',
        value: 1
      },
      {
        text: '100米女成绩',
        value: 2
      },
      {
        text: '铅球男成绩',
        value: 3
      },
      {
        text: '铅球女成绩',
        value: 4
      },
      {
        text: '立定跳男成绩',
        value: 5
      },
      {
        text: '立定跳女成绩',
        value: 6
      },
    ],
    sumScore: 0,
    value: '',
    projectItem: 0,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  touchStart(e) {
    const successList = this.data.successList
    successList.forEach(item => {
      item.show = false
    });
    //更新successList
    this.setData({
      successList
    })
    //设置初始位置的值为滑动的坐标
    this.setData({
      touchStartX: e.changedTouches[0].clientX
    })
  },
  touchEnd(e) {
    //获取当前点击index
    const index = e.currentTarget.dataset.index
    const successList = this.data.successList
    if (this.data.touchStartX < e.changedTouches[0].clientX + 200) {
      successList[index].show = true
    }
    if (this.data.touchStartX >= e.changedTouches[0].clientX) {
      successList[index].show = false
    }
    //更新successList
    this.setData({
      successList
    })
  },
  delSuccess(e) {
    //将index值作为参数传入,必须是index数组下标
    const {
      index
    } = e.currentTarget.dataset;
    //调用数组中的splice方法删除元素
    this.data.successList.splice(index, 1);
    //删除之后,重新赋值
    this.setData({
      successList: this.data.successList
    })
    var sum = 0
    this.data.successList.forEach(item => {
      sum += item.score
    });
    this.setData({
      sumScore: sum
    });
    Notify({
      type: 'success',
      message: '删除成功',
      duration: 500
    });
  },
  addSuccess() {
    if (this.data.successList.length === 8) {
      Notify({
        type: 'danger',
        message: '添加失败，最多只能添加 8 个项目',
        duration: 800
      });
      return
    }
    //调用数组中的push方法填加元素
    var success = {
      id: util.wxuuid(),
      projectItem: 0,
      success: null,
      score: 0,
      unit: '',
      show: false
    }
    this.data.successList.push(success);
    this.setData({
      successList: this.data.successList
    })
    Notify({
      type: 'success',
      message: '添加成功',
      duration: 500
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  bindKeySelect(event) {
    var sum = 0
    var that = this
    console.log(that)
    var id = event.currentTarget.dataset.id;
    var detail = event.detail
    this.data.successList.forEach(item => {
      if (item.id === id) {
        item.projectItem = detail
        var unit = ''
        switch (detail) {
          case 1:
          case 2:
            unit = '秒'
            break;
          case 3:
          case 4:
          case 5:
            unit = '米'
            break;
          default:
            unit = '米'
            break;
        }
        item.unit = unit
        if (item.success !== null && detail!== '') {
          item.score = that.calculate(item.success, item.projectItem);
        }
      }
      sum += item.score
    });
    this.dataRefresh(sum)
  },
  share() {
    Toast('分享ing...')
  },
  dataRefresh(sum){
    this.setData({
      successList: this.data.successList
    })
    this.setData({
      sumScore: sum
    });
  },
  bindKeyInput(event) {
    var that = this
    var id = event.currentTarget.dataset.id
    var detail = event.detail.value
    var sum = 0
    this.data.successList.forEach(item => {
      if (item.id === id) {
        item.success = detail
        if (item.projectItem !== 0 && detail!==null && detail!== '') {
          item.score = that.calculate(item.success, item.projectItem);
        }
      }
      sum += item.score
    });
    this.dataRefresh(sum)
  },
  //计算分数的方法 返回分数
  calculate: function (success, type) {
    var score = 0
    var maxFlag = false
    var standardList = this.data.standardList
    outer: for (let i = 0; i < standardList.length; i++) {
      const standard = standardList[i];
      switch (type) {
        case 1:
          if (success < 11) {
            maxFlag = true
          } else if (success <= standard.text1) {
            score = standard.code
          }
          break;
        case 2:
          if (success < 11) {
            maxFlag = true
          } else if (success <= standard.text2) {
            score = standard.code
          }
          break;
        case 3:
          if (success > 20) {
            maxFlag = true
          } else if (success >= standard.text3) {
            score = standard.code
          }
          break;
        case 4:
          if (success > 15) {
            maxFlag = true
          } else if (success >= standard.text4) {
            score = standard.code
          }
          break
        case 5:
          if (success > 4) {
            maxFlag = true
          } else if (success >= standard.text5) {
            score = standard.code
          }
          break
        case 6:
          if (success > 3.5) {
            maxFlag = true
          } else if (success >= standard.text6) {
            score = standard.code
          }
          break
        default:
          break
      }
      if (score !== 0) {
        break outer
      }
    }
    if (maxFlag) {
      Notify({
        type: 'warning',
        message: '非人哉',
        duration: 800
      });
    }
    if (!maxFlag && score === 0) {
      Notify({
        type: 'primary',
        message: '未通过最低成绩，请继续努力',
        duration: 1000
      });
    }
    return score
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})